package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.Owner.SignAdd;
import com.sand.dto.Owner.signAddAll;
import com.sand.dto.Owner.signSelect;
import com.sand.entity.buy.sign;
import com.sand.entity.shopping.shopping;
import com.sand.feign.ShoppingFeign;
import com.sand.mapper.SignMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.result.ResponseResult;
import com.sand.service.SignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SignServiceImpl extends ServiceImpl<SignMapper , sign> implements SignService {

    @Autowired
    private ShoppingFeign shoppingFeign;

    @Autowired
    private SignMapper signMapper;
    
    /**
     * 客户添加商品标记
     *
     * @param addAll
     * @return
     */
    @Override
    public Integer addAll(signAddAll addAll) {

        //存放要批量添加的数据
        ArrayList<sign> arrayList = new ArrayList<>();

        //获取需要添加的商品id和数量
        List<SignAdd> list = addAll.getList();

        //获取标记
        String s = addAll.getSign();

        for (int i = 0; i < list.size(); i++) {
            sign sign = new sign();

            //设置标记
            sign.setSign(s);

            //设置商品id
            sign.setShoppingid(list.get(i).getShoppingid());

            //设置数量
            sign.setAmount(list.get(i).getAmount());

            //添加到集合中
            arrayList.add(sign);

        }

        //批量添加数据
        saveBatch(arrayList);

        return 1;
    }


    /**
     * 客户根据标记删除对应的商品
     *
     * @param sign
     */
    @Override
    public Integer delete(String sign) {
        //构造条件
        LambdaQueryWrapper<sign> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(com.sand.entity.buy.sign::getSign , sign);

        //批量删除满足条件的数据
        boolean remove = remove(wrapper);

        return 1;
    }




    /**
     * 客户根据标记获取标记的商品
     *
     * @param sign
     */
    @Override
    public List<signSelect> select(String sign) {

        if (sign == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入标记");
        }

        LambdaQueryWrapper<com.sand.entity.buy.sign> wrapper = new LambdaQueryWrapper<sign>().eq(com.sand.entity.buy.sign::getSign, sign);

        List<com.sand.entity.buy.sign> list = signMapper.selectList(wrapper);

        if (list == null || list.isEmpty()){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"传入的标记有误无法查询到数据");
        }

        Integer[] ints = new Integer[list.size()];

        for (int i = 0; i < list.size(); i++) {
            sign s = list.get(i);
            ints[i] = s.getShoppingid();
        }

        //根据ids获取商品信息
        List<shopping> shoppingList = shoppingFeign.select(ints).getData();

        if (shoppingList == null){
            return null;
        }

        ArrayList<signSelect> signSelects = new ArrayList<>();

        //对应的商品设置数量
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < shoppingList.size(); j++) {
                if (Objects.equals(list.get(i).getShoppingid(), shoppingList.get(j).getId())){
                    signSelect signSelect = new signSelect();

                    shopping shopping = shoppingList.get(j);
                    Integer amount = list.get(i).getAmount();

                    signSelect.setAmount(amount);

                    BeanUtils.copyProperties(shopping,signSelect);

                    signSelects.add(signSelect);
                }
            }
        }


        return signSelects;
    }


}
